package cn.lyjuan.payment.third.http.api.vo.req;

import cn.lyjuan.payment.third.base.common.enums.impl.ChlEnum;
import cn.lyjuan.payment.third.base.pay.chl.impl.ali.bean.AliPayReq;
import cn.lyjuan.payment.third.base.pay.chl.impl.ali.enums.AliServiceEnum;
import cn.lyjuan.payment.third.base.pay.chl.impl.iap.bean.IapPayReq;
import cn.lyjuan.payment.third.base.pay.chl.impl.iap.enums.IosDevelopEnum;
import cn.lyjuan.payment.third.base.pay.chl.impl.weixin.bean.WxPayReq;
import cn.lyjuan.payment.third.base.pay.chl.impl.weixin.enums.WxTradeType;
import cn.lyjuan.payment.third.base.pay.vo.req.PayReq;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@ApiModel("交易订单数据")
public class OrderReq {
    @ApiModelProperty(value = "应用ID", required = true)
    @NotNull
    private Integer appId;

    @ApiModelProperty(value = "支付渠道", required = true)
    @NotNull
    private ChlEnum chlType;

    @ApiModelProperty(value = "交易流水号", required = true)
    @NotNull
    private String appNo;

    @ApiModelProperty(value = "商品标题", required = true)
    @NotNull
    private String goods;

    @ApiModelProperty(value = "应自定义参数", notes = "在通知和回调时原样返回")
    private String flag;

    @ApiModelProperty(value = "金额", notes = "单位：分", required = true)
    @NotNull
    @Min(1)
    @Max(9999999)
    private Integer amt;

    @ApiModelProperty(value = "通知地址", notes = "可自带参数，支持http和https")
    private String notifyUrl;

    @ApiModelProperty(value = "回调地址", notes = "在浏览器环境下，支付完成后会返回该网页，商家需要在该页面主动查询交易状态")
    private String returnUrl;

    @ApiModelProperty(value = "独立参数", notes = "第三方平台独立参数，详见对应的第三方文档说明")
    private PayParams params;

    @ApiModelProperty("MD5 32位签名")
    private String sign;

    @ApiModel("支付平台独立参数")
    public static class PayParams {
        @ApiModelProperty(value = "支付调用方式", notes = "支付宝")
        private AliServiceEnum service;

        @ApiModelProperty(value = "微信交易类型", notes = "微信")
        protected WxTradeType tradeType;

        @ApiModelProperty(value = "商品ID", notes = "微信")
        protected Long goodsId;

        @ApiModelProperty(value = "随机字符串", notes = "微信")
        protected String noncestr;

        @ApiModelProperty(value = "IOS开发类型", notes = "苹果")
        protected IosDevelopEnum devType;

//        @ApiModelProperty(value = "凭据", notes = "苹果")
//        protected String receipt;
//
//        @ApiModelProperty(value = "appStoreGoodsId", notes = "苹果商店 商品id")
//        protected String iosGoodsId;
//
//        @ApiModelProperty(value = "交易id", notes = "苹果商店")
//        protected String transactionId;

        private AliPayReq ali() {
            return new AliPayReq(service);
        }

        private WxPayReq wechat() {
            return new WxPayReq(this.tradeType, this.goodsId, noncestr);
        }

        private IapPayReq iap() {
            return new IapPayReq(devType);
        }

        /**
         * 获取参数
         *
         * @return
         */
        public PayReq.IPayReq params(ChlEnum chlType) {
            switch (chlType) {
                case ALI:
                    return ali();
                // 微信公众号与开放平台参数相同
                case WX_MP:
                case WX_OPEN:
                    return wechat();
                case IAP:
                    return iap();
            }
            return null;
        }

        public AliServiceEnum getService() {
            return service;
        }

        public void setService(AliServiceEnum service) {
            this.service = service;
        }

        public WxTradeType getTradeType() {
            return tradeType;
        }

        public void setTradeType(WxTradeType tradeType) {
            this.tradeType = tradeType;
        }

        public Long getGoodsId() {
            return goodsId;
        }

        public void setGoodsId(Long goodsId) {
            this.goodsId = goodsId;
        }

        public String getNoncestr() {
            return noncestr;
        }

        public void setNoncestr(String noncestr) {
            this.noncestr = noncestr;
        }

        public IosDevelopEnum getDevType() {
            return devType;
        }

        public void setDevType(IosDevelopEnum devType) {
            this.devType = devType;
        }

        @Override
        public String toString() {
            return "PayParams{" +
                    "service=" + service +
                    ", tradeType=" + tradeType +
                    ", goodsId=" + goodsId +
                    ", noncestr='" + noncestr + '\'' +
                    ", devType='" + devType + '\'' +
                    '}';
        }
    }

    public Integer getAppId() {
        return appId;
    }

    public void setAppId(Integer appId) {
        this.appId = appId;
    }

    public ChlEnum getChlType() {
        return chlType;
    }

    public void setChlType(ChlEnum chlType) {
        this.chlType = chlType;
    }

    public String getAppNo() {
        return appNo;
    }

    public void setAppNo(String appNo) {
        this.appNo = appNo;
    }

    public String getGoods() {
        return goods;
    }

    public void setGoods(String goods) {
        this.goods = goods;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public Integer getAmt() {
        return amt;
    }

    public void setAmt(Integer amt) {
        this.amt = amt;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public PayParams getParams() {
        return params;
    }

    public void setParams(PayParams params) {
        this.params = params;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    @Override
    public String toString() {
        return "OrderReq{" +
                "appId=" + appId +
                ", chlType=" + chlType +
                ", appNo='" + appNo + '\'' +
                ", goods='" + goods + '\'' +
                ", flag='" + flag + '\'' +
                ", amt=" + amt +
                ", notifyUrl='" + notifyUrl + '\'' +
                ", returnUrl='" + returnUrl + '\'' +
                ", params=" + params +
                ", sign='" + sign + '\'' +
                '}';
    }
}
